package cn.achao.csr.auth.controller;

import cn.achao.csr.api.model.dto.ZfbNotifyDTO;
import cn.achao.csr.api.model.entity.LoginUser;
import cn.achao.csr.auth.core.model.dto.AccountLoginDTO;
import cn.achao.csr.auth.core.model.dto.ZfbLoginDTO;
import cn.achao.csr.auth.service.LoginService;
import cn.achao.csr.base.resp.R;
import cn.achao.csr.security.service.TokenService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: achao
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/auth")
public class AuthController {

    private final LoginService loginService;
    private final TokenService tokenService;

    @PostMapping("/login")
    public R<?> login(@RequestBody @Validated AccountLoginDTO req) {
        // 用户登录
        LoginUser userInfo = loginService.login(req.getUserAccount(), req.getUserPwd());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    @PostMapping("/zfbLogin")
    public R<?> zfbLogin(@RequestBody @Validated ZfbLoginDTO req) {
        // 获取支付宝授权地址
        return R.ok(loginService.zfbLogin(req.getStatus(), req.getUserAccount()));
    }

    @PostMapping("/zfbNotify")
    public R<?> zfbNotify(@RequestBody @Validated ZfbNotifyDTO req) {
        LoginUser userInfo = loginService.zfbNotify(req);
        Map<String, Object> res = tokenService.createToken(userInfo);
        res.put("status", req.getStatus());
        return R.ok(res);
    }

    @GetMapping("/getCurrentUser")
    public R<?> getCurrentUser(@RequestParam("token") String token) {
        LoginUser loginUser = tokenService.getLoginUser(token);
        return R.ok(loginUser);
    }
}
